export default (Vue) => {
  Vue.directive('iosbugfixed', {
    inserted(el) {
      const ele = el
      function checkWxScroll() {
        let currentPosition
        const speed = 1
        // 页面滚动距离
        const timer = setInterval(() => {
          currentPosition = document.documentElement.scrollTop || document.body.scrollTop
          currentPosition -= speed
          // 页面向上滚动
          window.scrollTo(0, currentPosition)
          currentPosition += speed
          // 页面向下滚动
          window.scrollTo(0, currentPosition)
          clearInterval(timer)
        }, 1)
      }
      // 解决input、select被组件包装的场景
      const childInput = el.getElementsByTagName('input')
      const childSelect = el.getElementsByTagName('select')
      for (let i = 0; i < childInput.length; i += 1) {
        childInput[i].onblur = function temporaryRepair() {
          setTimeout(() => {
            // 当input失焦时，滚动一下页面就可以使页面恢复正常
            checkWxScroll()
          }, 200)
        }
      }
      for (let i = 0; i < childSelect.length; i += 1) {
        childSelect[i].onblur = function temporaryRepair() {
          setTimeout(() => {
            // 当input失焦时，滚动一下页面就可以使页面恢复正常
            checkWxScroll()
          }, 200)
        }
      }
      // 正常场景
      ele.onblur = function temporaryRepair() {
        setTimeout(() => {
          // 当input失焦时，滚动一下页面就可以使页面恢复正常
          checkWxScroll()
        }, 200)
      }
    }
  })
}
